package com.shop.front.dao.store;


import com.shop.jpa.BaseSQLDao;
import com.shop.jpa.support.QuerySQL;
import com.shop.front.vo.store.StoreProductReplyCountVo;
import com.shop.domain.store.StoreProductReply;
import com.shop.web.context.WebContext;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Repository;

@Repository
public class StoreProductReplySQLDao extends BaseSQLDao {

    public StoreProductReplyCountVo count(Long productId) {
        StringBuffer field = new StringBuffer("count(*) total_num")
                            .append(", ifnull(sum(case when `product_score` = 5 then 1 else 0 end), 0) good_num")
                            .append(", ifnull(sum(case when `product_score` in (3,4) then 1 else 0 end), 0) mid_num")
                            .append(", ifnull(sum(case when `product_score` in (1,2) then 1 else 0 end), 0) poor_num")
                            ;
        String from = "t_store_product_reply spr";
        String order = null;

        QuerySQL qs = new QuerySQL(field.toString(), from, order);
        qs.addWhereWithoutParams("spr.is_deleted = 0 ");
        qs.addWhereEq("spr.product_id", productId);

        return this.querySingle(qs, StoreProductReplyCountVo.class);
    }

    public Page<StoreProductReply> page(Long productId, Integer type) {
        String field = "*";
        String from = "t_store_product_reply spr";
        String order = "spr.id desc";

        QuerySQL qs = new QuerySQL(WebContext.getPageNo(), WebContext.getPageSize(), field, from, order);
        qs.addWhereWithoutParams("spr.is_deleted = 0 ");
        qs.addWhereEq("spr.product_id", productId);

        // 评论类型
        if(null != type) {
            if(1 == type) {
                qs.addWhereWithoutParams("spr.product_score in (5)");
            } else if(2 == type) {
                qs.addWhereWithoutParams("spr.product_score in (3,4)");
            } else if(3 == type) {
                qs.addWhereWithoutParams("spr.product_score in (1,2)");
            }
        }

        return this.page(qs, StoreProductReply.class);
    }
}
